package com.atguigu.daijia.rules.service.impl;

import com.atguigu.daijia.model.form.rules.ProfitsharingRuleRequest;
import com.atguigu.daijia.model.form.rules.ProfitsharingRuleRequestForm;
import com.atguigu.daijia.model.vo.rules.ProfitsharingRuleResponse;
import com.atguigu.daijia.model.vo.rules.ProfitsharingRuleResponseVo;
import com.atguigu.daijia.rules.mapper.ProfitsharingRuleMapper;
import com.atguigu.daijia.rules.service.ProfitsharingRuleService;
import com.atguigu.daijia.rules.utils.DroolsHelper;
import lombok.extern.slf4j.Slf4j;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class ProfitsharingRuleServiceImpl implements ProfitsharingRuleService {

    @Autowired
    private ProfitsharingRuleMapper rewardRuleMapper;

    //规则文件位置
    private static final String RULES_CUSTOMER_RULES_DRL = "rules/ProfitsharingRule.drl";

    //计算系统分账费用
    @Override
    public ProfitsharingRuleResponseVo calculateOrderProfitsharingFee(ProfitsharingRuleRequestForm form) {
        //输入对象
        ProfitsharingRuleRequest request = new ProfitsharingRuleRequest();
        request.setOrderAmount(form.getOrderAmount());//订单金额
        request.setOrderNum(form.getOrderNum());//当天完成的订单数

        //创建规则引擎对象
        KieSession kieSession = DroolsHelper.loadForRule(RULES_CUSTOMER_RULES_DRL);

        //返回对象
        ProfitsharingRuleResponse response = new ProfitsharingRuleResponse();
        kieSession.setGlobal("profitsharingRuleResponse",response);

        //设置对象，触发规则
        kieSession.insert(request);
        kieSession.fireAllRules();
        //终止会话
        kieSession.dispose();

        //封装返回对象
        ProfitsharingRuleResponseVo vo = new ProfitsharingRuleResponseVo();
        BeanUtils.copyProperties(response,vo);

        return vo;
    }
}
